package com.source.hotelbath.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.source.hotelbath.R;
import com.source.hotelbath.base.BizActivity;
import com.source.hotelbath.weight.LinePathView;

import java.io.IOException;

public class SignActivity extends BizActivity {


    private Button btn_clear;
    private LinePathView mPathView;
    private LinearLayout ll_header_left;
    private LinearLayout ll_header_right;
    private TextView tv_header_right;
    private Button btn_submit;

    @Override
    protected void onBaseCreate(Bundle var1) {
        setContentView(R.layout.activity_sign);
    }

    @Override
    protected void bindView(Bundle var1) {
        ll_header_left = (LinearLayout) findViewById(R.id.ll_header_left);

        ll_header_right = (LinearLayout) findViewById(R.id.ll_header_right);
        ll_header_right.setVisibility(View.GONE);
        tv_header_right = (TextView) findViewById(R.id.tv_header_right);
        tv_header_right.setText("提交签名");
        ll_header_right.setOnClickListener(this);
        ll_header_left.setOnClickListener(this);
        TextView tv_header_title= (TextView) findViewById(R.id.tv_header_title);
        tv_header_title.setText("签名");
        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_submit = (Button) findViewById(R.id.btn_submit);
        mPathView = (LinePathView) findViewById(R.id.view);
        ll_header_left.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view==btn_submit){
            if (mPathView.getTouched()) {
                verifyStoragePermissions();

            } else {

                Toast.makeText(SignActivity.this, "您没有签名~", Toast.LENGTH_SHORT).show();
            }
        }else if (view==ll_header_left){
            finish();
        }else if (view==btn_clear){
            mPathView.clear();
        }
    }

    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    protected final int REQUEST_CODE_CAMERA = 1001;

    public void verifyStoragePermissions() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                new AlertDialog.Builder(this)
                        .setMessage("申请存储权限")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //申请相机权限
                                ActivityCompat.requestPermissions(SignActivity.this,
                                        PERMISSIONS_STORAGE, REQUEST_CODE_CAMERA);
                            }
                        })
                        .show();
            } else {
                //申请相机权限
                ActivityCompat.requestPermissions(this,
                        PERMISSIONS_STORAGE, REQUEST_CODE_CAMERA);
            }
        }else {
            try {
                Log.d("path", Environment.getExternalStorageDirectory().getAbsolutePath()+"/qm.png");
                mPathView.save(Environment.getExternalStorageDirectory().getAbsolutePath()+"/qm.png", true, 10);
                setResult(100);
                finish();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){

            case REQUEST_CODE_CAMERA:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted
                    try {
                        Log.d("path",Environment.getExternalStorageDirectory().getAbsolutePath()+"/qm.png");
                        mPathView.save(Environment.getExternalStorageDirectory().getAbsolutePath()+"/qm.png", false, 10);
                        setResult(100);
                        finish();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                        Toast.makeText(this, "存储权限已被禁止", Toast.LENGTH_SHORT).show();
                    }
                    // Permission Denied
//                    ToastHelper.toast("很遗憾你把相机权限禁用了。请务必开启相机权限享受我们提供的服务吧。");
                }
                break;
        }
    }

}
